package br.com.colegio.dao.storage;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

import javax.jdo.Query;

public class Storage
{
	@SuppressWarnings("unchecked")
	public static Collection<Object> select(String model, String order)
	{
		Collection<Object> list = null;

		try
		{
			Query q = PMF.getPersistenceManager().newQuery(Class.forName(model));

			if (order != null && !order.trim().equals(""))
				q.setOrdering(order);

			Collection<Object> temp = (Collection<Object>) q.execute();
			if (temp != null && temp.size() >= 1)
			{
				list = new HashSet<Object>();
				for (Object obj : temp)
					list.add(obj);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	public static Collection<Object> select(String model, String where, String order)
	{
		try
		{
			Query q = PMF.getPersistenceManager().newQuery(Class.forName(model), where);

			if (order != null && !order.trim().equals(""))
				q.setOrdering(order);

			return (Collection<Object>)q.execute();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public static Collection<Object> select(String model, String filter, String params, Map<String, Object> parVal, String order)
	{
		try
		{
			Query q = PMF.getPersistenceManager().newQuery(Class.forName(model));
			q.setFilter(filter);
			q.declareParameters(params);
			if (order != null && !order.trim().equals(""))
				q.setOrdering(order);

			return (Collection<Object>)q.executeWithMap(parVal);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
/*
	public static List<Object> selectLike(String model, Map<String, Object> parVal)
	{
		try
		{
			com.google.appengine.api.datastore.Query q = new com.google.appengine.api.datastore.Query();

			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

			for (String k : parVal.keySet())
				q.addFilter(k,
					com.google.appengine.api.datastore.Query.FilterOperator.GREATER_THAN_OR_EQUAL,
					parVal.get(k));

			return ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
*/
	public static Object get(String model, Long id)
	{
		try
		{
			return PMF.getPersistenceManager().getObjectById(Class.forName(model), id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}

	public static Object save(Object obj)
	{
		PMF.getPersistenceManager().close();
		
		Object o = PMF.getPersistenceManager().makePersistent(obj);

		return o;
	}

	public static Object saveAndWait(Object obj)
	{
		PMF.getPersistenceManager().makePersistent(obj);

		return obj;
	}

	public static void closePM(Object obj)
	{
		PMF.getPersistenceManager().close();
	}

	public static void delete(String model, Long id)
	{
		try
		{
			PMF.getPersistenceManager().deletePersistent(PMF.getPersistenceManager().getObjectById(Class.forName(model), id));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
